// SerialPortNotifier.cpp : main source file for SerialPortNotifier.exe
//

#include "stdafx.h"

#include "resource.h"

#include "MainDlg.h"

#define APP_GUID TEXT("Global\\{B747F83B-1059-46ca-8216-B171EA0386D9}")

class LimitSingleInstance {
private:
	DWORD last_error_;
	HANDLE mutex_;

public:
	inline explicit LimitSingleInstance(const std::wstring &app_guid) {
		mutex_ = ::CreateMutex(NULL, FALSE, app_guid.c_str());
		last_error_ = ::GetLastError();
	}

	~LimitSingleInstance() {
		if (mutex_ != NULL) {
			::CloseHandle(mutex_);
			mutex_ = NULL;
		}
	}

	bool isInstanceRunning() const {
		return last_error_ == ERROR_ALREADY_EXISTS;
	}
};

LimitSingleInstance _SingleInstance(APP_GUID);
CAppModule _Module;

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*lpstrCmdLine*/, int /*nCmdShow*/)
{
	HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to 
// make the EXE free threaded. This means that calls come in on a random RPC thread.
//	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	ATLASSERT(SUCCEEDED(hRes));

	// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
	::DefWindowProc(NULL, 0, 0, 0L);

	AtlInitCommonControls(ICC_BAR_CLASSES);	// add flags to support other controls

	hRes = _Module.Init(NULL, hInstance);
	ATLASSERT(SUCCEEDED(hRes));

	int nRet = 0;
	// BLOCK: Run application
	{
		CMainDlg dlgMain;
		nRet = dlgMain.DoModal();
	}

	_Module.Term();
	::CoUninitialize();

	return nRet;
}
